package com.cskaoyan.mall.service.implement;

import com.cskaoyan.mall.dao.GoodsDao;
import com.cskaoyan.mall.dao.implement.GoodsDaoImpl;
import com.cskaoyan.mall.model.bo.replyBO;
import com.cskaoyan.mall.model.sql.Goods;
import com.cskaoyan.mall.model.sql.Type;
import com.cskaoyan.mall.model.bo.GoodsBO;
import com.cskaoyan.mall.model.bo.SpecBO;
import com.cskaoyan.mall.model.vo.*;
import com.cskaoyan.mall.service.GoodsService;

import java.util.ArrayList;
import java.util.List;

public class GoodServiceImpl implements GoodsService {

    private GoodsDao goodsDao = new GoodsDaoImpl();

    @Override
    public List<Type> getType() {
        return goodsDao.getType();
    }

    @Override
    public List<TypeGoodsVO> getGoodsType(String typeId) {
        return goodsDao.getGoodsType(typeId);
    }

    /**
     * 新增商品表price、stockNum需要通过specList运算得到
     * 1.保存数据到商品表
     * 2.拿到商品表刚刚插入的商品id
     * 3.将该id以及spec数据保存到spec规格表
     * @param goodsBO
     */
    @Override
    public void addGoods(GoodsBO goodsBO) {
        List<SpecBO> specList = goodsBO.getSpecList();
        Double price = specList.get(0).getUnitPrice();
        int stockNum = specList.get(0).getStockNum();
        for (int i = 1; i <specList.size() ; i++) {
            if(price > specList.get(i).getUnitPrice()){
                price = specList.get(i).getUnitPrice(); //最小的unitPrice
            }
            if (stockNum < specList.get(i).getStockNum()){
                stockNum = specList.get(i).getStockNum(); //最大的库存量StockNum
            }

        }

        Goods goods = new Goods(null,
                goodsBO.getImg(),
                goodsBO.getName(),
                price,
                goodsBO.getTypeId(),
                stockNum,
                goodsBO.getDesc());
        goodsDao.addGoods(goods);
        //此id就是新增商品的id
        int id = goodsDao.lastInsertId();
        List<Spec> specs = new ArrayList<>();
        for (SpecBO specBO:specList
             ) {
            Spec spec = new Spec(null, specBO.getSpecName(), specBO.getStockNum(),
                    specBO.getUnitPrice(), id);
            specs.add(spec);
        }
        goodsDao.addSpecs(specs);
    }

    /**
     * 业务逻辑：
     *  首先获取GoodsInfoVO
     *  从数据库中获取 id 对应的goods 表，存储在 GoodsInfoVO中
     *  获取数据库 id 对应的Spec表，存储在 GoodsInfoVO中
     * @param id
     * @return
     */
    @Override
    public GoodsInfoVO getGoodsInfo(String id) {
        GoodsInfoVO goodsInfoVO = new GoodsInfoVO();
        //①
        InfoGoodVO infoGoodVO = goodsDao.getInfoGoodVO(id);
        goodsInfoVO.setGoods(infoGoodVO);
        //②
        List<SpecVO> specVOList=goodsDao.getSpecVO(id);
        goodsInfoVO.setSpecs(specVOList);
        return goodsInfoVO;
    }

    @Override
    public void deleteGoods(String id) {
        //删除对应goods表
        goodsDao.deleteGoods(id);
        //同时删除对应的specs表
        goodsDao.deleteSpec(id);
    }

    @Override
    public void addType(Type type) {
        goodsDao.addType(type);
    }

    @Override
    public void deleteType(String id) {
        goodsDao.deleteType(id);
    }

    @Override
    public List<noReplyMsgVO> noReplyMsg() {
        List<noReplyMsgVO> noReplyMsgVOS = new ArrayList<>();//这个是用来返回给Servlet的VO对象
        //先对noReplyMsgVOS赋予部分值（除了goods和users的name）
        //再对List里面每一个对象对应goodsId和userId查找 对应的name
        noReplyMsgVOS = goodsDao.noReplyMsg(noReplyMsgVOS);
        if(noReplyMsgVOS.size()==0) try {
            throw new Exception();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return noReplyMsgVOS;
    }

    @Override
    public List<noReplyMsgVO> ReplyMsg() {
        List<noReplyMsgVO> noReplyMsgVOS = new ArrayList<>();//这个是用来返回给Servlet的VO对象
        //先对noReplyMsgVOS赋予部分值（除了goods和users的name）
        //再对List里面每一个对象对应goodsId和userId查找 对应的name
        noReplyMsgVOS = goodsDao.ReplyMsg(noReplyMsgVOS);
        if(noReplyMsgVOS.size()==0) try {
            throw new Exception();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return noReplyMsgVOS;
    }

    @Override
    public void reply(replyBO replybo) {
        goodsDao.reply(replybo);
    }

    @Override
    public List<Goods> searchGoods(String keyword) {
        return goodsDao.searchGoods(keyword);
    }
}
